<template>
  <div>
    <j-modal title="审计综合考核指标表" :visible="visible" ok-text="" :fullscreen="true" @close="visible = false">
      <div style="width: 100%">
        <a-table
          v-if="tableShow"
          class="edit-list"
          ref="table"
          size="middle"
          bordered
          width="100vw"
          :pagination="false"
          :columns="columns"
          :dataSource="dataSource"
          :scroll="{ x: 250, y: 678 }"
        >
          <template slot="name" slot-scope="name">
            <div style="width: 100%; height: 45px; overflow-x: hidden; overflow-y: auto">{{ name }}</div>
          </template>
          <template v-for="col in customRenderList" v-slot:[col]="text, record">
            <div :key="col" v-if="col != 'thirdRemarks' && col != 'secondRemarks' && col != 'oneRemarks'">
              <a-input-number
                :key="col"
                style="width: 50px"
                :disabled="true"
                @change="handleChange(record, col)"
                v-model="record[col]"
              />
            </div>
            <div :key="col" v-else-if="col == 'thirdRemarks' || col == 'secondRemarks' || col == 'oneRemarks'">
              <a-textarea
                :key="col"
                :disabled="true"
                :auto-size="{ minRows: 1, maxRows: 3 }"
                @change="handleChange(record, col)"
                v-model="record[col]"
              />
            </div>
            <div :key="col" v-else>
              <span>{{ record[col] }}</span>
            </div>
          </template>
        </a-table>
      </div>

      <template slot="footer">
        <a-button @click="handleCancel">取消</a-button>
        <!-- <a-popconfirm style="margin-left: 8px" :disabled="disableSubmit" title="确定驳回该考核?" @confirm="handleRej">
          <a-button type="primary" :disabled="disableSubmit">驳回</a-button>
        </a-popconfirm> -->
        <a-button :disabled="disableSubmit" type="primary" @click="openAuditModel(record)">驳回</a-button>
<!--        <a-button :disabled="disableSubmit" type="primary" @click="handleTableSave(0)">保存</a-button>-->
        <a-popconfirm
          style="margin-left: 8px"
          :disabled="disableSubmit"
          title="确定提交并同意该考核?"
          @confirm="handleTableSave(1)"
        >
          <a-button key="submit" :disabled="disableSubmit" type="primary">提交</a-button>
        </a-popconfirm>
        <!-- <a-button key="submit" :disabled="disableSubmit" @click="handleTableSave(1)" type="primary">提交</a-button> -->
      </template>
    </j-modal>

    <!-- <addTemp ref="modalForm" @openModelView="handleAddTemp"></addTemp> -->
    <auditModel ref="creatModel" @ok="handleRej"></auditModel>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
// import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
// import addTemp from './components/addTemp.vue'
import columns1 from '../columns1'
import columns2 from '../columns2'
import { getAction, postAction } from '@/api/manage'
import auditModel from '../auditModel.vue'

export default {
  name: 'editModel',
  mixins: [JeecgListMixin],
  components: { auditModel },
  props: ['role'],
  data() {
    return {
      visible: false,
      dataSource: [],
      disableSubmit: false,
      tableShow: false,
      addBottonShow: true,
      columns: columns1(this),
      checkInfoId: '',
      templateId: '',
      // TODO 可编辑列分类
      editableList: {
        3: ['thirdAdd', 'thirdBuckle', 'thirdTotal', 'thirdRemarks'],
        2: ['secondAdd', 'secondBuckle', 'secondTotal', 'secondRemarks'],
        1: ['oneAdd', 'oneBuckle', 'oneTotal', 'oneRemarks'],
      },
      customRenderList: [
        // 'thirdAdd',
        // 'thirdBuckle',
        // 'thirdTotal',
        // 'thirdRemarks',
        // 'secondAdd',
        // 'secondBuckle',
        // 'secondTotal',
        // 'secondRemarks',
        // 'oneAdd',
        // 'oneBuckle',
        // 'oneTotal',
        // 'oneRemarks'
      ],
      url: {
        list: '/performcheck/checkDeptScore/queryScoreList',
        tablelist: '/performcheck/checkDeptScore/queryScoreList',
        role: '/performcheck/checkDeptInfo/queryScoreList',
        template: '/performcheck/checkProjectInfo/queryTemplateProject',
        save: '/performcheck/checkDeptScore/saveScore',
        add: '/performcheck/checkDeptInfo/auditPass',
        rej: '/performcheck/checkDeptInfo/auditReject',
        // pass: '/performcheck/checkDeptInfo/auditPass',
      },
      disableMixinCreated: false,
    }
  },
  computed: {},
  mounted() {
    this.setRole()
  },
  methods: {
    openAuditModel(record) {
      if (record) {
        this.$refs.creatModel.rowData = record
      }
      this.$refs.creatModel.visible = true
    },
    async handleRej(param) {
      let params ={
        ...param,
        id: this.checkInfoId
      }
      const data = await postAction(this.url.rej, params)
      if (data.success) {
        this.$message.success(data.message)
        this.visible = false
        this.$emit('load')
      } else {
        this.$message.error(data.message)
      }
    },
    async handleTableSave(type) {
      let params = {
        checkInfoId: this.checkInfoId,
        templateId: this.templateId,
        listData: this.dataSource,
      }
      const data = await postAction(type == 1 ? this.url.add : this.url.save, params)
      if (data.success) {
        this.$message.success(data.message)
        this.visible = false
        this.$emit('updated')
      } else {
        this.$message.error(data.message)
      }
    },
    handleAddTemp(param) {
      const { id } = param || ''
      this.templateId = id
      this.getTableTemp(id)
      this.tableShow = true
    },
    // 获取模版
    async getTableTemp(id) {
      const { result } = await getAction(`${this.url.template}?id=${id}`)
      this.dataSource = result
    },
    setRole() {
      if (this.role == '1') {
        this.customRenderList = this.editableList['1']
        if (this.subRole == '2') {
          this.columns = columns2(this)
        } else if (this.subRole == '3') {
          this.columns = columns1(this)
        }
      }

      if (this.role == '2') {
        this.customRenderList = this.editableList['2']
        if (this.subRole == '2') {
          this.columns = columns2(this)
        } else if (this.subRole == '3') {
          this.columns = columns1(this)
        }
      }

      if (this.role == '3') {
        this.customRenderList = this.editableList['3']
        if (this.subRole == '2') {
          this.columns = columns2(this)
        } else if (this.subRole == '3') {
          this.columns = columns1(this)
        }
      }
    },
    edit(record) {
      this.dataSource = []
      this.checkInfoId = Object.assign({}, record).id
      this.templateId = record.templateId
      this.subRole = record.orgLevel
      this.setRole()
      if (record.templateId !== null && record.templateId !== '') {
        this.addBottonShow = false
        this.tableShow = true
        // this.customRenderList = []
        this.url.list = `${this.url.tablelist}?checkInfoId=${record.id}&templateId=${this.templateId}`
        this.loadData()
      } else {
        this.addBottonShow = true
        this.tableShow = false
      }
      //   this.loadData()
      this.visible = true
    },

    handleCancel(e) {
      this.visible = false
      this.addBottonShow = true
      this.tableShow = false
    },
    openCreatModel() {
      this.$refs.modalForm.visible = true
    },
    // 计算合计
    handleChange(row, col) {},
    setHjCol(col) {
      let rows = this.dataSource.length
      let hjNum = 0
      for (let i = 0; i < rows; i++) {
        hjNum = hjNum + parseFloat(this.dataSource[i][col] || 0)
      }
      this.dataSource[0][col] = hjNum
    },
    setHj(col, column) {
      let rows = this.dataSource.length - 1
      let num = 0
      let hjNum = 0
      for (let i = 1; i < rows; i++) {
        num = num + parseFloat(this.dataSource[i][col] || 0)
        hjNum = hjNum + parseFloat(this.dataSource[i][column] || 0)
      }
      this.dataSource[0][col] = num
      this.dataSource[0][column] = hjNum
    },
    // 合并数据
    mergeCells(text, data, key, index) {
      // 上一行该列数据是否一样
      if (index !== 0 && text === data[index - 1][key]) {
        return 0
      }
      let rowSpan = 1
      // 判断下一行是否相等
      for (let i = index + 1; i < data.length; i++) {
        if (text !== data[i][key]) {
          break
        }
        rowSpan++
      }
      return rowSpan
    },
    // 合并加求和
    mergeCellsandSum(text, data, key, index) {
      let sum = 0;
      let rowSpan = 1
      console.log(data);
      // 判断下一行是否相等
      for (let i = index + 1; i < data.length; i++) {
        // sum += Number.parseInt(data.)
        rowSpan++
      }
      return {rowSpan,sum}
    },
    setBr(text, data, key, index) {
      if (data[key] != null) {
        let contents = data[key].split('/')
        let item = null
        for (let i = 0; i < contents.length; i++) {
          if (i == 0) {
            item = `(${i + 1}) ${contents[i]}`
          } else {
            item = (
              <span>
                {item}
                <br />
                {`(${i + 1}) ${contents[i]}`}
              </span>
            )
          }
        }
        return item
      }
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
.edit-list {
  /deep/ .ant-table-tbody {
    tr {
      td {
        padding: 10px !important;
      }
    }
  }
}
</style>